package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.cache.newanswers.INewAnswersCache;
import com.conversationboard.cache.newanswers.NewAnswersCaches;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;

@WebServlet(name = "AdminDropThreadFromNewPostsControllerServlet", urlPatterns = "/Admin/AdminDropThreadFromNewPostsControllerServlet")
public class AdminDropThreadFromNewPostsControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -3318600516391241773L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {
			int boardId = Integer.parseInt(request.getParameter("boardid"));
			int threadId = Integer.parseInt(request.getParameter("threadid"));

			User user = User.get(request.getUserPrincipal());

			if (!(user.isBoardAdmin(boardId) && SecurityChecks.passes(request, user, this))) {
				return;
			}

			/*
			 * Remove from its own new answers cache and the site new answers
			 * cache if it's in there too
			 */

			INewAnswersCache cache = NewAnswersCaches.getInstance(boardId);
			cache.removeThread(boardId, threadId);

			Site.getSiteNewAnswers().removeThread(boardId, threadId);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/NewAnswersControllerServlet?boardid=" + boardId + "&amp;threadid=" + threadId);
			dispatcher.forward(request, response);

		} catch (NumberFormatException e) {
		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
